<?php	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class BackendModel extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->library(array('session', 'pagination'));
        $this->load->helper('url');
        $this->load->database();
    }

    /**
     * Created by PhpStorm.
     * User: 杨森源
     * Date: 2016/07/16
     * Time: 14:30
     */

    //根据分类得到新闻公告的总条数
    function get_newsInform_rows($category_id){
        //$query=$this->db->query("select newsinforms.*,workers.wName from newsinforms,workers where typeId='$category_id' and newsinforms.workerId=workers.id;");
        $query=$this->db->query("select * from newsinforms where typeId='$category_id'");
        return $query->num_rows();
    }

    //得到栏目名称
    function get_category($category_id){
        $query = $this->db->query("select name from category where id='$category_id'");
        return $query->row();
    }

    //分页查询
    function get_newsInformList($category_id,$lim,$per_page){
        //$query = $this->db->query("select newsinforms.*,workers.wName from newsinforms,workers where typeId='$category_id' and newsinforms.workerId=workers.id order by top desc,time desc,id desc limit $lim,$per_page");
        $query = $this->db->query("select * from newsinforms where typeId='$category_id' order by top desc,time desc,id desc limit $lim,$per_page");
        return $query->result();
    }

    //根据id得到单条数据
    function get_newsInfo($id){
        $query = $this->db->query("select * from newsinforms where id='$id'");
        return $query->result();
    }

    //修改文章
    function edit_news(){
        $id=$this->input->post("id");
        $title=$this->input->post("title");
        $publisher=$this->input->post("publisher");
        $time=$this->input->post("time");
        $top=$this->input->post("up");
        $workerId=$this->session->userdata("workerId");
        if($top=="置顶"){
            $top=1;
        }elseif($top=="不置顶"){
            $top=0;
        }
        $content=$this->input->post("content");
        preg_match_all("/<img.*src=\"(.*)\".*\>/isU", $content, $matches);
        $image=$matches[1][0];
        $data=array(
            'title'=>$title,
            'workerId'=>$workerId,
            'publisher'=>$publisher,
            'time'=>$time,
            'top'=>$top,
            'content'=>$content,
            'picture'=>$image
        );
        $this->db->where('id',$id);
        $this->db->update('newsinforms', $data);
    }

    //添加文章
    function add_news($typeId){
        $title=$this->input->post("title");
        $publisher=$this->input->post("publisher");
        $time=$this->input->post("time");
        $top=$this->input->post("up");
        $workerId=$this->session->userdata("workerId");
        if($top=="置顶"){
            $top=1;
        }elseif($top=="不置顶"){
            $top=0;
        }
        $content=$this->input->post("content");
        preg_match_all("/<img.*src=\"(.*)\".*\>/isU", $content, $matches);
        $image=$matches[1][0];
        $data=array(
            'title'=>$title,
            'workerId'=>$workerId,
            'publisher'=>$publisher,
            'time'=>$time,
            'top'=>$top,
            'content'=>$content,
            'typeId'=>$typeId,
            'picture'=>$image
        );
        $this->db->insert('newsinforms', $data);
    }

    //删除新闻通知
    function delete_NewsInform($type,$id){
        $this->db->where('id',$id);
        $res = $this->db->delete('newsinforms');
        if($res){
            return true;
        }else{
            return false;
        }
    }
    //置顶新闻公告
    function top_newsInform($top,$id){
        if($top==0){
            $data=array(
                'top'=>1
            );
        }else{
            $data=array(
                'top'=>0
            );
        }
        $this->db->where('id',$id);
        $this->db->update('newsinforms',$data);
    }

    //获取辅导员
    function get_schoolAssistant(){
        $wSchool = $this->session->userdata("wSchool");
        $wInstitution = $this->session->userdata("wInstitution");
        $sql = "select distinct wInstitution from workers where wSchool = ? and wPower = ?";
        $res = $this->db->query($sql,array("$wSchool","A"));
        $arr = $res->result_array();
        return $arr;
    }

    //获取教师
    function get_schoolTeacher(){
        $wSchool = $this->session->userdata("wSchool");
        $wInstitution = $this->session->userdata("wInstitution");
        $sql = "select distinct wInstitution from workers where wSchool = ? and wPower = ?";
        $res = $this->db->query($sql,array("$wSchool","T"));
        $arr = $res->result_array();
        return $arr;
    }
    //获取学生
    function get_schoolStudent(){
        $wSchool = $this->session->userdata("wSchool");
        $wInstitution = $this->session->userdata("wInstitution");
        $sql = "select distinct sInstitution from students where sSchool = ?";
        $res = $this->db->query($sql,array("$wSchool"));
        $arr = $res->result_array();
        return $arr;
    }

    //获取教职工列表
    function get_schoolWorkerInformation($wSchool,$wInstitution,$wPower,$offset,$page_size){
        //echo $wPower;exit();
        $sql = "select * from workers where wSchool=? and wInstitution=? and wPower=? limit ?,?";
        $res = $this->db->query($sql,array("$wSchool","$wInstitution","$wPower",$offset,$page_size));
        $arr = $res->result_array();
        return $arr;
    }
    //获取学生列表
    function get_schoolStudentInformation($sSchool,$sInstitution,$sMajor,$offset,$page_size){
        $sql = "select * from students where sSchool=? and sInstitution=? and sMajor=? order by sGrade desc limit ?,?";
        $res = $this->db->query($sql,array("$sSchool","$sInstitution","$sMajor",$offset,$page_size));
        $arr = $res->result_array();
        return $arr;
    }

    function get_Major($sSchool,$sInstitution){
        $sql = "select DISTINCT sMajor from students where sSchool='$sSchool' and sInstitution = '$sInstitution'";
        $res = $this->db->query($sql);
        return $res->result_array();
    }

    function get_ajaxPage($offset,$page_size,$school,$institution,$wPower){
        $sql = "select * from workers where wSchool='$school'and wInstitution='$institution' and wPower='$wPower' limit $offset,$page_size";
        $res = $this->db->query($sql);
        return $res->result_array();
    }

    //根据老师的id获取全部信息
    public function getWorkerInfo($workerID)
    {
        $query = $this->db->get_where('workers', array('id' => $workerID));
        return $query->result();
    }

    //按修改密码
    public function updatePwd($id){
        $newPsw=md5($this->input->post("newPsw"));
        $this->db->where('id',$id);
        $this->db->update('workers',array("wPassword"=>$newPsw));
    }



}